Back to course home
0% completed
Vote For New Content
Google Hard
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Maximum Points You Can Obtain from Cards (medium)
Problem Statement
You are given several cards arranged in a single row, where ith card has cardPoints[i] points.
In a single step, you can take one card from the start or from the end of the row. You have to take exactly k cards.
The score is the sum of the points of the cards you have withdrawn.
Given the cardPoints array containing integers and the integer k, return the maximum score you can obtain.
Examples
-
Example 1:
- Input: cardPoints =
[10, 14, 7, 2, 8, 9, 4], k =3 - Expected Output:
31 - Justification: The best strategy is to take the cards with 10, 14, and 7 points. The maximum score will be 10 + 14 + 7 = 31.
- Input: cardPoints =
-
Example 2:
- Input: cardPoints =
[1, 2, 3, 4, 5, 6], k =2 - Expected Output:
11 - Justification: Taking the two cards from the right end (5 and 6) gives us the highest score. The sum here is higher than any other combination of 2 cards.
- Input: cardPoints =
-
Example 3:
- Input: cardPoints =
[91, 24, 36, 48, 72, 19, 29], k =4 - Expected Output:
211 - Justification: The optimal move is to take 91 from the left, then 29, 19, and 72 from the right, achieving the maximum possible score.
- Input: cardPoints =
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: 01 Matrix
Solution: Maximum Points You Can Obtain from Cards
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself